﻿using System;
using System.Collections.Generic;
using System.Web;
using UniversityManagementSystem.DAL;
using UniversityManagementSystem.Gateway;

namespace UniversityManagementSystem.Manager
{
    [Serializable]
    public class TeacherManager
    {
        TeacherGateway gateway = new TeacherGateway();
        public List<string> GetAllDesignations()
        {
            return gateway.GetAllDesignations();
        }
        public bool IsNullOrBlank(String text)
        {
            return text == null || text.Trim().Length == 0;
        }
        public bool isNumber(string courseCredit)
        {
            bool isNum = false;
            double Num;
            isNum = double.TryParse(courseCredit, out Num);
            return isNum;
        }

        public string SaveTeacher(Teacher aTeacher)
        {
            string saveTeacherMessage = "";
            try
            {
                if (IsNullOrBlank(aTeacher.TeacherName) || IsNullOrBlank(aTeacher.TeacherAddress) || IsNullOrBlank(aTeacher.TeacherEmail) || IsNullOrBlank(aTeacher.TeacherContact) || IsNullOrBlank(aTeacher.TeacherDesignation) || IsNullOrBlank(aTeacher.TeacherDeparment))
                {

                    saveTeacherMessage = "Please fill up the fields correctly.";

                }
                else
                {
                    aTeacher.TeacherId = GetTeacherId(aTeacher);
                    if (gateway.CheckEmail(aTeacher.TeacherEmail))
                    {
                        saveTeacherMessage = "Email already exists.";
                    }
                    else if (gateway.SaveTeacher(aTeacher))
                    {
                        saveTeacherMessage = "Teacher has been added successfully.";
                    }
                    else
                    {
                        saveTeacherMessage = "Teacher cannot be added right now,Please try again later.";
                    }
                }
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return saveTeacherMessage;
        }

        private string GetTeacherId(Teacher aTeacher)
        {
            string noOfTeacher;
            noOfTeacher = gateway.GetTeacherNumber(aTeacher.TeacherDesignation);
            if(aTeacher.TeacherDesignation == "Professor")
            {
                return "T"+"01"+noOfTeacher+aTeacher.TeacherDeparment;
            }
            else if (aTeacher.TeacherDesignation == "Assistant Professor")
            {
                return "T" + "02" + noOfTeacher + aTeacher.TeacherDeparment;
            }
            else
            {
                return  "T" + "03" + noOfTeacher + aTeacher.TeacherDeparment;
            }
        }

        public List<Teacher> GetTeachers(string department)
        {
            return gateway.GetTeachers(department);
        }
    }
}